<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Creating Custom Form Markup Using Zend_Form_Decorator - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.form.decorators.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.form.decorators.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.form.forms.html">Creating Forms Using Zend_Form</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.form.html">Zend_Form</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.form.standardElements.html">Standard Form Elements Shipped With Zend Framework</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.form.decorators" class="section"><div class="info"><h1 class="title">Creating Custom Form Markup Using Zend_Form_Decorator</h1></div>
    

    <p class="para">
        Rendering a form object is completely optional -- you do not need to use
        <span class="classname">Zend_Form</span>&#039;s render() methods at all. However, if you do,
        decorators are used to render the various form objects.
    </p>

    <p class="para">
        An arbitrary number of decorators may be attached to each item
        (elements, display groups, sub forms, or the form object itself);
        however, only one decorator of a given type may be attached to each
        item. Decorators are called in the order they are registered. Depending
        on the decorator, it may replace the content passed to it, or append or
        prepend the content.
    </p>

    <p class="para">
        Object state is set via configuration options passed to the constructor
        or the decorator&#039;s  <span class="methodname">setOptions()</span> method. When creating
        decorators via an item&#039;s  <span class="methodname">addDecorator()</span> or related methods,
        options may be passed as an argument to the method. These can be used to
        specify placement, a separator to use between passed in content and
        newly generated content, and whatever options the decorator supports.
    </p>

    <p class="para">
        Before each decorator&#039;s  <span class="methodname">render()</span> method is called, the
        current item is set in the decorator using  <span class="methodname">setElement()</span>,
        giving the decorator awareness of the item being rendered. This allows
        you to create decorators that only render specific portions of the item
        -- such as the label, the value, error messages, etc. By stringing
        together several decorators that render specific element segments, you
        can build complex markup representing the entire item.
    </p>

    <div class="section" id="zend.form.decorators.operation"><div class="info"><h1 class="title">Operation</h1></div>
        

        <p class="para">
            To configure a decorator, pass an array of options or a
            <span class="classname">Zend_Config</span> object to its constructor, an array to
             <span class="methodname">setOptions()</span>, or a <span class="classname">Zend_Config</span> object to
             <span class="methodname">setConfig()</span>.
        </p>

        <p class="para">
            Standard options include:
        </p>

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">
                    <span class="property">placement</span>: Placement can be either &#039;append&#039; or
                    &#039;prepend&#039; (case insensitive), and indicates whether content
                    passed to  <span class="methodname">render()</span> will be appended or
                    prepended, respectively. In the case that a decorator
                    replaces the content, this setting is ignored. The default
                    setting is to append.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    <span class="property">separator</span>: The separator is used between the
                    content passed to  <span class="methodname">render()</span> and new content
                    generated by the decorator, or between items rendered by the
                    decorator (e.g. FormElements uses the separator between each
                    item rendered). In the case that a decorator replaces the
                    content, this setting may be ignored. The default value is
                    <b><tt>PHP_EOL</tt></b>.
                </p>
            </li>
        </ul>

        <p class="para">
            The decorator interface specifies methods for interacting with
            options. These include:
        </p>

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">
                     <span class="methodname">setOption($key, $value)</span>: set a single option.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">getOption($key)</span>: retrieve a single option value.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">getOptions()</span>: retrieve all options.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">removeOption($key)</span>: remove a single option.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">clearOptions()</span>: remove all options.
                </p>
            </li>
        </ul>

        <p class="para">
            Decorators are meant to interact with the various
            <span class="classname">Zend_Form</span> class types: <span class="classname">Zend_Form</span>,
            <span class="classname">Zend_Form_Element</span>, <span class="classname">Zend_Form_DisplayGroup</span>,
            and all classes deriving from them. The method
             <span class="methodname">setElement()</span> allows you to set the object the
            decorator is currently working with, and  <span class="methodname">getElement()</span>
            is used to retrieve it.
        </p>

        <p class="para">
            Each decorator&#039;s  <span class="methodname">render()</span> method accepts a string,
            <var class="varname">$content</var>. When the first decorator is called, this
            string is typically empty, while on subsequent calls it will be
            populated. Based on the type of decorator and the options passed in,
            the decorator will either replace this string, prepend the string,
            or append the string; an optional separator will be used in the
            latter two situations.
        </p>
    </div>

    <div class="section" id="zend.form.decorators.standard"><div class="info"><h1 class="title">Standard Decorators</h1></div>
        

        <p class="para">
            <span class="classname">Zend_Form</span> ships with many standard decorators; see
            <a href="zend.form.standardDecorators.html" class="link">the chapter on Standard
            Decorators</a> for details.
        </p>
    </div>

    <div class="section" id="zend.form.decorators.custom"><div class="info"><h1 class="title">Custom Decorators</h1></div>
        

        <p class="para">
            If you find your rendering needs are complex or need heavy
            customization, you should consider creating a custom decorator.
        </p>

        <p class="para">
            Decorators need only implement
            <span class="classname">Zend_Form_Decorator_Interface</span>. The interface specifies the
            following:
        </p>

        <pre class="programlisting brush: php">
interface Zend_Form_Decorator_Interface
{
    public function __construct($options = null);
    public function setElement($element);
    public function getElement();
    public function setOptions(array $options);
    public function setConfig(Zend_Config $config);
    public function setOption($key, $value);
    public function getOption($key);
    public function getOptions();
    public function removeOption($key);
    public function clearOptions();
    public function render($content);
}
</pre>


        <p class="para">
            To make this simpler, you can simply extend
            <span class="classname">Zend_Form_Decorator_Abstract</span>, which implements all methods
            except  <span class="methodname">render()</span>.
        </p>

        <p class="para">
            As an example, let&#039;s say you want to reduce the number of
            decorators you use, and build a &quot;composite&quot; decorator to take care
            of rendering the label, element, any error messages, and description
            in an <acronym class="acronym">HTML</acronym> &#039;div&#039;. You might build such a &#039;Composite&#039;
            decorator as follows:
        </p>

        <pre class="programlisting brush: php">
class My_Decorator_Composite extends Zend_Form_Decorator_Abstract
{
    public function buildLabel()
    {
        $element = $this-&gt;getElement();
        $label = $element-&gt;getLabel();
        if ($translator = $element-&gt;getTranslator()) {
            $label = $translator-&gt;translate($label);
        }
        if ($element-&gt;isRequired()) {
            $label .= &#039;*&#039;;
        }
        $label .= &#039;:&#039;;
        return $element-&gt;getView()
                       -&gt;formLabel($element-&gt;getName(), $label);
    }

    public function buildInput()
    {
        $element = $this-&gt;getElement();
        $helper  = $element-&gt;helper;
        return $element-&gt;getView()-&gt;$helper(
            $element-&gt;getName(),
            $element-&gt;getValue(),
            $element-&gt;getAttribs(),
            $element-&gt;options
        );
    }

    public function buildErrors()
    {
        $element  = $this-&gt;getElement();
        $messages = $element-&gt;getMessages();
        if (empty($messages)) {
            return &#039;&#039;;
        }
        return &#039;&lt;div class=&quot;errors&quot;&gt;&#039; .
               $element-&gt;getView()-&gt;formErrors($messages) . &#039;&lt;/div&gt;&#039;;
    }

    public function buildDescription()
    {
        $element = $this-&gt;getElement();
        $desc    = $element-&gt;getDescription();
        if (empty($desc)) {
            return &#039;&#039;;
        }
        return &#039;&lt;div class=&quot;description&quot;&gt;&#039; . $desc . &#039;&lt;/div&gt;&#039;;
    }

    public function render($content)
    {
        $element = $this-&gt;getElement();
        if (!$element instanceof Zend_Form_Element) {
            return $content;
        }
        if (null === $element-&gt;getView()) {
            return $content;
        }

        $separator = $this-&gt;getSeparator();
        $placement = $this-&gt;getPlacement();
        $label     = $this-&gt;buildLabel();
        $input     = $this-&gt;buildInput();
        $errors    = $this-&gt;buildErrors();
        $desc      = $this-&gt;buildDescription();

        $output = &#039;&lt;div class=&quot;form element&quot;&gt;&#039;
                . $label
                . $input
                . $errors
                . $desc
                . &#039;&lt;/div&gt;&#039;

        switch ($placement) {
            case (self::PREPEND):
                return $output . $separator . $content;
            case (self::APPEND):
            default:
                return $content . $separator . $output;
        }
    }
}
</pre>


        <p class="para">
            You can then place this in the decorator path:
        </p>

        <pre class="programlisting brush: php">
// for an element:
$element-&gt;addPrefixPath(&#039;My_Decorator&#039;,
                        &#039;My/Decorator/&#039;,
                        &#039;decorator&#039;);

// for all elements:
$form-&gt;addElementPrefixPath(&#039;My_Decorator&#039;,
                            &#039;My/Decorator/&#039;,
                            &#039;decorator&#039;);
</pre>


        <p class="para">
            You can then specify this decorator as &#039;Composite&#039; and attach it to
            an element:
        </p>

        <pre class="programlisting brush: php">
// Overwrite existing decorators with this single one:
$element-&gt;setDecorators(array(&#039;Composite&#039;));
</pre>


        <p class="para">
            While this example showed how to create a decorator that renders
            complex output from several element properties, you can also create
            decorators that handle a single aspect of an element; the
            &#039;Decorator&#039; and &#039;Label&#039; decorators are excellent examples of this
            practice. Doing so allows you to mix and match decorators to achieve
            complex output -- and also override single aspects of decoration to
            customize for your needs.
        </p>

        <p class="para">
            For example, if you wanted to simply display that an error occurred
            when validating an element, but not display each of the individual
            validation error messages, you might create your own &#039;Errors&#039;
            decorator:
        </p>

        <pre class="programlisting brush: php">
class My_Decorator_Errors
{
    public function render($content = &#039;&#039;)
    {
        $output = &#039;&lt;div class=&quot;errors&quot;&gt;The value you provided was invalid;
            please try again&lt;/div&gt;&#039;;

        $placement = $this-&gt;getPlacement();
        $separator = $this-&gt;getSeparator();

        switch ($placement) {
            case &#039;PREPEND&#039;:
                return $output . $separator . $content;
            case &#039;APPEND&#039;:
            default:
                return $content . $separator . $output;
        }
    }
}
</pre>


        <p class="para">
            In this particular example, because the decorator&#039;s final segment,
            &#039;Errors&#039;, matches the same as <span class="classname">Zend_Form_Decorator_Errors</span>,
            it will be rendered <em class="emphasis">in place of</em> that decorator
            -- meaning you would not need to change any decorators to modify the
            output. By naming your decorators after existing standard
            decorators, you can modify decoration without needing to modify your
            elements&#039; decorators.
        </p>
    </div>

    <div class="section" id="zend.form.decorators.individual"><div class="info"><h1 class="title">Rendering Individual Decorators</h1></div>
        

        <p class="para">
            Since decorators can target distinct metadata of the element or form
            they decorate, it&#039;s often useful to render one individual decorator
            at a time. This behavior is possible via method
            overloading in each major form class type (forms, sub form, display
            group, element).
        </p>

        <p class="para">
            To do so, simply call  <span class="methodname">render[DecoratorName]()</span>, where
            &quot;[DecoratorName]&quot; is the &quot;short name&quot; of your decorator; optionally,
            you can pass in content you want decorated. For example:
        </p>

        <pre class="programlisting brush: php">
// render just the element label decorator:
echo $element-&gt;renderLabel();

// render just the display group fieldset, with some content:
echo $group-&gt;renderFieldset(&#039;fieldset content&#039;);

// render just the form HTML tag, with some content:
echo $form-&gt;renderHtmlTag(&#039;wrap this content&#039;);
</pre>


        <p class="para">
            If the decorator does not exist, an exception is raised.
        </p>

        <p class="para">
            This can be useful particularly when rendering a form with the
            ViewScript decorator; each element can use its attached decorators
            to generate content, but with fine-grained control.
        </p>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.form.forms.html">Creating Forms Using Zend_Form</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.form.html">Zend_Form</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.form.standardElements.html">Standard Form Elements Shipped With Zend Framework</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.form.html">Zend_Form</a></li>
  <li><a href="zend.form.introduction.html">Zend_Form</a></li>
  <li><a href="zend.form.quickstart.html">Zend_Form Quick Start</a></li>
  <li><a href="zend.form.elements.html">Creating Form Elements Using Zend_Form_Element</a></li>
  <li><a href="zend.form.forms.html">Creating Forms Using Zend_Form</a></li>
  <li class="active"><a href="zend.form.decorators.html">Creating Custom Form Markup Using Zend_Form_Decorator</a></li>
  <li><a href="zend.form.standardElements.html">Standard Form Elements Shipped With Zend Framework</a></li>
  <li><a href="zend.form.standardDecorators.html">Standard Form Decorators Shipped With Zend Framework</a></li>
  <li><a href="zend.form.i18n.html">Internationalization of Zend_Form</a></li>
  <li><a href="zend.form.advanced.html">Advanced Zend_Form Usage</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>